package com.huahua.api.service.entertainment.website.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huahua.api.entity.entertainment.website.param.WebsiteGenerateParam;
import com.huahua.api.entity.entertainment.website.pojo.EntertainmentWebsiteGenerateParam;
import com.huahua.api.entity.entertainment.website.pojo.EntertainmentWebsiteGenerateTemplate;
import com.huahua.api.entity.entertainment.website.vo.EntertainmentWebsiteGenerateTemplateVO;
import com.huahua.api.exception.CommonException;
import com.huahua.api.repository.entertainment.website.EntertainmentWebsiteGenerateTemplateRepository;
import com.huahua.api.service.entertainment.website.EntertainmentWebsiteGenerateParamService;
import com.huahua.api.service.entertainment.website.EntertainmentWebsiteGenerateSettingService;
import com.huahua.api.service.entertainment.website.EntertainmentWebsiteGenerateTemplateService;
import lombok.RequiredArgsConstructor;
import org.dromara.hutool.core.bean.BeanUtil;
import org.dromara.hutool.core.util.RandomUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @author LoveHuaHua
 * @date 2023年08月21日 21:07
 * @description believe in yourself
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Lazy))
public class EntertainmentWebsiteGenerateTemplateServiceImpl implements EntertainmentWebsiteGenerateTemplateService {

    private final EntertainmentWebsiteGenerateTemplateRepository templateRepository;
    private final EntertainmentWebsiteGenerateParamService entertainmentWebsiteGenerateParamService;
    private final EntertainmentWebsiteGenerateSettingService entertainmentWebsiteGenerateSettingService;
    private final ObjectMapper objectMapper;

    /**
     * 查询参数
     *
     * @param param 参数
     * @return {@link JsonNode}
     */
    @Override
    public Mono<JsonNode> queryParam(WebsiteGenerateParam param) {
        String uuid = param.getUuid();
        return entertainmentWebsiteGenerateParamService.findByUuid(uuid)
                .switchIfEmpty(Mono.error(new CommonException("uuid不存在，请重新生成！")))
                .map(generateParam -> {
                    try {
                        return objectMapper.readValue(generateParam.getJson(), JsonNode.class);
                    } catch (Exception e) {
                        throw new CommonException("解析JSON失败: " + e.getMessage());
                    }
                });
    }

    /**
     * 生成网页
     *
     * @param param 参数
     * @return {@link String}
     */
    @Override
    public Mono<String> generate(EntertainmentWebsiteGenerateParam param) {
        String uuid = RandomUtil.randomNumbers(32);
        param.setUuid(uuid);
        return entertainmentWebsiteGenerateParamService.save(param)
                .map(savedParam -> uuid);
    }

    /**
     * 查询模板信息
     *
     * @param param 参数
     * @return {@link EntertainmentWebsiteGenerateTemplateVO}
     */
    @Override
    public Mono<EntertainmentWebsiteGenerateTemplateVO> queryTemplateInfo(EntertainmentWebsiteGenerateTemplate param) {
        return templateRepository.findById(param.getId())
                .switchIfEmpty(Mono.error(new CommonException("抱歉您选择的模板已删除，请重新选择！")))
                .flatMap(template ->
                        entertainmentWebsiteGenerateSettingService.findByTemplateId(param.getId())
                                .collectList()
                                .map(settings -> {
                                    EntertainmentWebsiteGenerateTemplateVO vo = new EntertainmentWebsiteGenerateTemplateVO();
                                    BeanUtil.copyProperties(template, vo);
                                    vo.setParams(settings);
                                    return vo;
                                })
                );
    }

    @Override
    public Mono<EntertainmentWebsiteGenerateTemplate> findById(Integer id) {
        return templateRepository.findById(id);
    }

    @Override
    public Flux<EntertainmentWebsiteGenerateTemplate> findAll() {
        return templateRepository.findAll();
    }

    @Override
    public Flux<EntertainmentWebsiteGenerateTemplate> findByType(Integer type) {
        return templateRepository.findByType(type);
    }
}
